#!/usr/bin/python
import cgi
import cgitb
import os
from pygallib import usersession
from pygallib import layoutlib
from pygallib import albumlib
from pygallib import userlib
from pygallib import picturelib

# Enable debug print
cgitb.enable()

# Printing Headers
logged_in = usersession.logged_in()
fields = cgi.FieldStorage()
if logged_in:
  session_user = usersession.get_session_user()

if "id" in fields:
  album_id = int(fields["id"].value)
  # Get album information
  album = albumlib.get_album(album_id)
  # Get user who created album
  user = userlib.get_user_by_id(album["user_id"])
  user_authorized = False
  if logged_in:
    if (session_user["user_id"] == user["user_id"] or
        session_user["root"] == 1):
      user_authorized = True

headers = {}
headers["Content-type"] = "text/html"
layoutlib.print_http_headers(headers)
layoutlib.print_page_open("PyGallery - Home")
print("<body>")
menu = []
menu.append(("Home", "gallery.cgi"))
layoutlib.print_page_header(menu, session_user)

print('<div id="page_body">')
if user_authorized:
  # delete pictures first
  (picture_cur, conn) = albumlib.get_album_pictures(album_id)
  """
  for picture in picture_cur:
    picturelib.remove_picture(album_id, 
        user["username"], 
        picture["picture_id"], conn=conn)
  """
  picture_cur.close()
  albumlib.remove_album(album_id, user["user_id"], conn=conn)
  conn.close()
  print('<p>Album %s deleted, click ' % (album["name"]) +
        '<a href="gallery.cgi">here</a> for the gallery.')
else:
  print("<p>You are not authorized to do that.</p>")
print('</div>')

layoutlib.print_page_close()
